package com.ruoyi.web.controller.front;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.utils.FileUploadUtils;
import com.ruoyi.utils.JwtUtils;
import com.ruoyi.utils.MyThreadLocal;
import com.ruoyi.vpn.domain.VpnCarmi;
import com.ruoyi.vpn.domain.VpnPlan;
import com.ruoyi.vpn.domain.VpnUser;
import com.ruoyi.vpn.service.IVpnCarmiService;
import com.ruoyi.vpn.service.IVpnPlanService;
import com.ruoyi.vpn.service.IVpnUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;


@Anonymous
@RestController
@RequestMapping("/front")
public class FrontUserController  extends BaseController {


    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private IVpnUserService vpnUserService;
    @Autowired
    private IVpnCarmiService VpnCarmiService;
    @Resource(name = "myFileUpLoadUtils")
    private FileUploadUtils fileUploadUtils;

    @Autowired
    private IVpnPlanService vpnPlanService;

    @PostMapping("login")
    public AjaxResult login(@RequestBody(required = false) VpnUser vpnUser) {
        System.out.println("vpnUser = " + vpnUser);
        if (vpnUser.getUsername() == null || vpnUser.getPassword() == null || vpnUser.getPassword().equals("") || vpnUser.getUsername().equals("")){
            return AjaxResult.error("用户名或密码错误");
        }
            List<VpnUser> vpnUsers = vpnUserService.selectVpnUserListStrict(vpnUser);
        if (vpnUsers.size() == 0) {
            return AjaxResult.error("用户名或密码错误");
        }
        VpnUser vpnUser1 = vpnUsers.get(0);
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", vpnUser1.getUserId());
        map.put("username", vpnUser1.getUsername());
        return AjaxResult.success(JwtUtils.generateToken(map));
    }
    /*
    * 用户头像上传
    * */
    @RequestMapping("/imageUpload")
    public AjaxResult fileUpLoad(@RequestParam("file") MultipartFile multipartFile) {
        System.out.println(666);

        String url = fileUploadUtils.upload(multipartFile);
        System.out.println(url);
        VpnUser vpnUser = new VpnUser();
        vpnUser.setUserId(Long.valueOf(MyThreadLocal.getData()));
        vpnUser.setImgUrl(url);
        int i = vpnUserService.updateVpnUser(vpnUser);
        if (i!=0){
            return AjaxResult.success(url);
        }
        return AjaxResult.error("更新失败");
    }
    /*
    * 检测用户登录*/
    @RequestMapping("/validateToken")
    public AjaxResult testToken() {
//        HttpServletRequest servletRequest1 = (HttpServletRequest) servletRequest;
//        String token = servletRequest1.getHeader("token");
//        Claims claims = jwtUtils.validateToken(token);
//        System.out.println("claims = " + claims);
        return AjaxResult.success("IS_LOGIN");
    }

    //登录后获取用户信息
    @RequestMapping("/getVpnUserByToken")
    public AjaxResult getVpnUserByLocalToken(String token){

        Integer id = MyThreadLocal.getData();
        VpnUser vpnUser = vpnUserService.selectVpnUserByUserId(Long.valueOf(id));
        return AjaxResult.success(vpnUser);
    }

    /*
    注册新用户
     */
    @RequestMapping("/register")
    public AjaxResult getVpnUserByLocalToken(@RequestBody(required = false) VpnUser vpnUser){
        vpnUser.setExpirationTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));

        vpnUser.setBandwidth(0L);
        System.out.println(vpnUser);
        int i = vpnUserService.insertVpnUser(vpnUser);
        if (i !=0){
            return AjaxResult.success(vpnUser);
        }
        return AjaxResult.error("创建失败");
    }

    //使用卡密充值
    @PostMapping("rechargeCarmi")
    public AjaxResult rechargeCarmi(@RequestBody(required = false) VpnCarmi vpnCarmi){

        Integer flag = VpnCarmiService.rechargeCarmi(vpnCarmi);

        if (flag !=0) return AjaxResult.success("充值成功");
        return AjaxResult.error("卡密有误");
    }

    @GetMapping("/palnList")
    public TableDataInfo list(VpnPlan vpnPlan)
    {
        startPage();
        List<VpnPlan> list = vpnPlanService.selectVpnPlanList(vpnPlan);
        return getDataTable(list);
    }



}



